package com.douwong.student.fragment;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.activity.ClassTestActivity;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseFragment;
import com.douwong.student.interfaces.ClassTestInterface;
import com.douwong.student.model.ResTestProblemsInfo;
import com.douwong.student.model.TestInfos;
import com.douwong.student.popuwindow.ShowSubjectDetailPopuWindow;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.UIUtils;
import com.douwong.student.view.MTextView;

/**
 * @autor yichao
 * @time 2017/4/516:07
 * @des 判断题
 */
@SuppressLint("ValidFragment")
public class JudgeFragment extends BaseFragment implements ClassTestInterface {
    private ClassTestActivity mClassTestActivity;
    private ResTestProblemsInfo.TopicItemBean mTopicItemBean;
    private View mContentView;
    private ImageView mIvHead;
    private  MTextView mMtTopic;
    private  LinearLayout mLlTitleRoot;
private  TextView mTv_judgment_right,mTv_judgment_wrong;
    private String mJudgMentLastAnswer="";
    private ShowSubjectDetailPopuWindow mPoDetailPopu;

    public JudgeFragment(ClassTestActivity classTestActivity, ResTestProblemsInfo.TopicItemBean topicItemBean) {

       this. mClassTestActivity = classTestActivity;
        this.mTopicItemBean = topicItemBean;
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        mContentView = UIUtils.inflate(getActivity(), R.layout.judgment_questions);
        mIvHead = (ImageView) mContentView.findViewById(R.id.iv_answer_show_pic);
        mMtTopic = (MTextView) mContentView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContentView.findViewById(R.id.ll_choicefragme_title_root);
        mTv_judgment_right = (TextView) mContentView.findViewById(R.id.tv_Judgment_right_question);
        mTv_judgment_wrong = (TextView) mContentView.findViewById(R.id.tv_Judgment_wrong_question);

        return mContentView;
    }

    @Override
    protected void initData() {
        super.initData();
        boolean showIcon = ImagFilterUtil.isShowIcon(mTopicItemBean);
        if (showIcon){
            mIvHead.setVisibility(View.VISIBLE);

        }
        //设置题目
        ImagFilterUtil.filterMTImage(mMtTopic,mTopicItemBean.getBody());
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mTv_judgment_right.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                v.setSelected(true);
                if (v.isSelected()) {

                    mJudgMentLastAnswer = "A";

                }
                mTv_judgment_wrong.setSelected(false);

            }
        });

        mTv_judgment_wrong.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mTv_judgment_right.setSelected(false);
                v.setSelected(true);
                if (v.isSelected()) {
                    mJudgMentLastAnswer = "B";

                }
            }
        });
        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                mPoDetailPopu = new ShowSubjectDetailPopuWindow(mClassTestActivity,
                        mTopicItemBean  );
                mPoDetailPopu.showAtLocation(getView(), Gravity.CENTER, 0, 0);

            }
        });
    }

    @Override
    public TestInfos getAnswerInfo() {
        if (mJudgMentLastAnswer.equals("")){
            BaseApplication.noDoTestSum++;
            return new TestInfos(mTopicItemBean.getId(),0,new String[]{mJudgMentLastAnswer});
        }else {
            double sore=0;
            if (mJudgMentLastAnswer.equals(mTopicItemBean.getAnswers().get(0))) {
                sore = mTopicItemBean.getScore();
            }
            return new TestInfos(mTopicItemBean.getId(),sore,new String[]{mJudgMentLastAnswer});
        }
    }

    @Override
    public void setCommitStatue() {
        mTv_judgment_right.setClickable(false);
        mTv_judgment_wrong.setClickable(false);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPoDetailPopu!=null && mPoDetailPopu.isShowing()){
            mPoDetailPopu.dismiss();
        }
    }
}
